home *** CD-ROM | disk | FTP | other *** search
/ Aminet 34 / Aminet 34 (2000)(Schatztruhe)[!][Dec 1999].iso / Aminet / dev / lang / SlimPython.lha / SlimPython / Objects / abstract.c next >
Encoding:
C/C++ Source or Header  |  1999-10-21  |  27.1 KB  |  1,454 lines

  1. /***********************************************************
  2. Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
  3. The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its
  8. documentation for any purpose and without fee is hereby granted,
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI or Corporation for National Research Initiatives or
  13. CNRI not be used in advertising or publicity pertaining to
  14. distribution of the software without specific, written prior
  15. permission.
  16.  
  17. While CWI is the initial source for this software, a modified version
  18. is made available by the Corporation for National Research Initiatives
  19. (CNRI) at the Internet address ftp://ftp.python.org.
  20.  
  21. STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
  22. REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
  23. MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
  24. CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
  25. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
  26. PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  27. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  28. PERFORMANCE OF THIS SOFTWARE.
  29.  
  30. ******************************************************************/
  31.  
  32. /* Abstract Object Interface (many thanks to Jim Fulton) */
  33.  
  34. #include "Python.h"
  35. #include <ctype.h>
  36. #include "protos/abstract.h"
  37.  
  38. /* Shorthands to return certain errors */
  39.  
  40. static PyObject *
  41. type_error(msg)
  42.     char *msg;
  43. {
  44.     PyErr_SetString(PyExc_TypeError, msg);
  45.     return NULL;
  46. }
  47.  
  48. static PyObject *
  49. null_error()
  50. {
  51.     if (!PyErr_Occurred())
  52.         PyErr_SetString(PyExc_SystemError,
  53.                 "null argument to internal routine");
  54.     return NULL;
  55. }
  56.  
  57. /* Copied with modifications from stropmodule.c: atoi, atof, atol */
  58.  
  59. static PyObject *
  60. int_from_string(v)
  61.     PyObject *v;
  62. {
  63.     char *s, *end;
  64.     long x;
  65.     char buffer[256]; /* For errors */
  66.  
  67.     s = PyString_AS_STRING(v);
  68.     while (*s && isspace(Py_CHARMASK(*s)))
  69.         s++;
  70.     errno = 0;
  71.     x = PyOS_strtol(s, &end, 10);
  72.     if (end == s || !isdigit(end[-1]))
  73.         goto bad;
  74.     while (*end && isspace(Py_CHARMASK(*end)))
  75.         end++;
  76.     if (*end != '\0') {
  77.   bad:
  78.         sprintf(buffer, "invalid literal for int(): %.200s", s);
  79.         PyErr_SetString(PyExc_ValueError, buffer);
  80.         return NULL;
  81.     }
  82.     else if (end != PyString_AS_STRING(v) + PyString_GET_SIZE(v)) {
  83.         PyErr_SetString(PyExc_ValueError,
  84.                 "null byte in argument for int()");
  85.         return NULL;
  86.     }
  87.     else if (errno != 0) {
  88.         sprintf(buffer, "int() literal too large: %.200s", s);
  89.         PyErr_SetString(PyExc_ValueError, buffer);
  90.         return NULL;
  91.     }
  92.     return PyInt_FromLong(x);
  93. }
  94.  
  95. static PyObject *
  96. float_from_string(v)
  97.     PyObject *v;
  98. {
  99.     extern double strtod Py_PROTO((const char *, char **));
  100.     char *s, *last, *end;
  101.     double x;
  102.     char buffer[256]; /* For errors */
  103.  
  104.     s = PyString_AS_STRING(v);
  105.     last = s + PyString_GET_SIZE(v);
  106.     while (*s && isspace(Py_CHARMASK(*s)))
  107.         s++;
  108.     if (s[0] == '\0') {
  109.         PyErr_SetString(PyExc_ValueError, "empty string for float()");
  110.         return NULL;
  111.     }
  112.     errno = 0;
  113.     PyFPE_START_PROTECT("float_from_string", return 0)
  114.     x = strtod(s, &end);
  115.     PyFPE_END_PROTECT(x)
  116.     /* Believe it or not, Solaris 2.6 can move end *beyond* the null
  117.        byte at the end of the string, when the input is inf(inity) */
  118.     if (end > last)
  119.         end = last;
  120.     while (*end && isspace(Py_CHARMASK(*end)))
  121.         end++;
  122.     if (*end != '\0') {
  123.         sprintf(buffer, "invalid literal for float(): %.200s", s);
  124.         PyErr_SetString(PyExc_ValueError, buffer);
  125.         return NULL;
  126.     }
  127.     else if (end != PyString_AS_STRING(v) + PyString_GET_SIZE(v)) {
  128.         PyErr_SetString(PyExc_ValueError,
  129.                 "null byte in argument for float()");
  130.         return NULL;
  131.     }
  132.     else if (errno != 0) {
  133.         sprintf(buffer, "float() literal too large: %.200s", s);
  134.         PyErr_SetString(PyExc_ValueError, buffer);
  135.         return NULL;
  136.     }
  137.     return PyFloat_FromDouble(x);
  138. }
  139.  
  140. /* Operations on any object */
  141.  
  142. int
  143. PyObject_Cmp(o1, o2, result)
  144.     PyObject *o1;
  145.     PyObject *o2;
  146.     int *result;
  147. {
  148.     int r;
  149.  
  150.     if (o1 == NULL || o2 == NULL) {
  151.         null_error();
  152.         return -1;
  153.     }
  154.     r = PyObject_Compare(o1, o2);
  155.     if (PyErr_Occurred())
  156.         return -1;
  157.     *result = r;
  158.     return 0;
  159. }
  160.  
  161. PyObject *
  162. PyObject_Type(o)
  163.     PyObject *o;
  164. {
  165.     PyObject *v;
  166.  
  167.     if (o == NULL)
  168.         return null_error();
  169.     v = (PyObject *)o->ob_type;
  170.     Py_INCREF(v);
  171.     return v;
  172. }
  173.  
  174. int
  175. PyObject_Length(o)
  176.     PyObject *o;
  177. {
  178.     PySequenceMethods *m;
  179.  
  180.     if (o == NULL) {
  181.         null_error();
  182.         return -1;
  183.     }
  184.  
  185.     m = o->ob_type->tp_as_sequence;
  186.     if (m && m->sq_length)
  187.         return m->sq_length(o);
  188.  
  189.     return PyMapping_Length(o);
  190. }
  191.  
  192. PyObject *
  193. PyObject_GetItem(o, key)
  194.     PyObject *o;
  195.     PyObject *key;
  196. {
  197.     PyMappingMethods *m;
  198.  
  199.     if (o == NULL || key == NULL)
  200.         return null_error();
  201.  
  202.     m = o->ob_type->tp_as_mapping;
  203.     if (m && m->mp_subscript)
  204.         return m->mp_subscript(o, key);
  205.  
  206.     if (o->ob_type->tp_as_sequence) {
  207.         if (PyInt_Check(key))
  208.             return PySequence_GetItem(o, PyInt_AsLong(key));
  209.         return type_error("sequence index must be integer");
  210.     }
  211.  
  212.     return type_error("unsubscriptable object");
  213. }
  214.  
  215. int
  216. PyObject_SetItem(o, key, value)
  217.     PyObject *o;
  218.     PyObject *key;
  219.     PyObject *value;
  220. {
  221.     PyMappingMethods *m;
  222.  
  223.     if (o == NULL || key == NULL || value == NULL) {
  224.         null_error();
  225.         return -1;
  226.     }
  227.     m = o->ob_type->tp_as_mapping;
  228.     if (m && m->mp_ass_subscript)
  229.         return m->mp_ass_subscript(o, key, value);
  230.  
  231.     if (o->ob_type->tp_as_sequence) {
  232.         if (PyInt_Check(key))
  233.             return PySequence_SetItem(o, PyInt_AsLong(key), value);
  234.         type_error("sequence index must be integer");
  235.         return -1;
  236.     }
  237.  
  238.     type_error("object does not support item assignment");
  239.     return -1;
  240. }
  241.  
  242. int
  243. PyObject_DelItem(o, key)
  244.     PyObject *o;
  245.     PyObject *key;
  246. {
  247.     PyMappingMethods *m;
  248.  
  249.     if (o == NULL || key == NULL) {
  250.         null_error();
  251.         return -1;
  252.     }
  253.     m = o->ob_type->tp_as_mapping;
  254.     if (m && m->mp_ass_subscript)
  255.         return m->mp_ass_subscript(o, key, (PyObject*)NULL);
  256.  
  257.     if (o->ob_type->tp_as_sequence) {
  258.         if (PyInt_Check(key))
  259.             return PySequence_DelItem(o, PyInt_AsLong(key));
  260.         type_error("sequence index must be integer");
  261.         return -1;
  262.     }
  263.  
  264.     type_error("object does not support item deletion");
  265.     return -1;
  266. }
  267.  
  268. /* Operations on numbers */
  269.  
  270. int
  271. PyNumber_Check(o)
  272.     PyObject *o;
  273. {
  274.     return o && o->ob_type->tp_as_number;
  275. }
  276.  
  277. /* Binary operators */
  278.  
  279. #define BINOP(v, w, opname, ropname, thisfunc) \
  280.     if (PyInstance_Check(v) || PyInstance_Check(w)) \
  281.         return PyInstance_DoBinOp(v, w, opname, ropname, thisfunc)
  282.  
  283. PyObject *
  284. PyNumber_Or(v, w)
  285.     PyObject *v, *w;
  286. {
  287.         extern int PyNumber_Coerce();
  288.  
  289.     BINOP(v, w, "__or__", "__ror__", PyNumber_Or);
  290.     if (v->ob_type->tp_as_number != NULL) {
  291.         PyObject *x = NULL;
  292.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  293.         if (PyNumber_Coerce(&v, &w) != 0)
  294.             return NULL;
  295.         if ((f = v->ob_type->tp_as_number->nb_or) != NULL)
  296.             x = (*f)(v, w);
  297.         Py_DECREF(v);
  298.         Py_DECREF(w);
  299.         if (f != NULL)
  300.             return x;
  301.     }
  302.     return type_error("bad operand type(s) for |");
  303. }
  304.  
  305. PyObject *
  306. PyNumber_Xor(v, w)
  307.     PyObject *v, *w;
  308. {
  309.         extern int PyNumber_Coerce();
  310.  
  311.     BINOP(v, w, "__xor__", "__rxor__", PyNumber_Xor);
  312.     if (v->ob_type->tp_as_number != NULL) {
  313.         PyObject *x = NULL;
  314.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  315.         if (PyNumber_Coerce(&v, &w) != 0)
  316.             return NULL;
  317.         if ((f = v->ob_type->tp_as_number->nb_xor) != NULL)
  318.             x = (*f)(v, w);
  319.         Py_DECREF(v);
  320.         Py_DECREF(w);
  321.         if (f != NULL)
  322.             return x;
  323.     }
  324.     return type_error("bad operand type(s) for ^");
  325. }
  326.  
  327. PyObject *
  328. PyNumber_And(v, w)
  329.     PyObject *v, *w;
  330. {
  331.     BINOP(v, w, "__and__", "__rand__", PyNumber_And);
  332.     if (v->ob_type->tp_as_number != NULL) {
  333.         PyObject *x = NULL;
  334.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  335.         if (PyNumber_Coerce(&v, &w) != 0)
  336.             return NULL;
  337.         if ((f = v->ob_type->tp_as_number->nb_and) != NULL)
  338.             x = (*f)(v, w);
  339.         Py_DECREF(v);
  340.         Py_DECREF(w);
  341.         if (f != NULL)
  342.             return x;
  343.     }
  344.     return type_error("bad operand type(s) for &");
  345. }
  346.  
  347. PyObject *
  348. PyNumber_Lshift(v, w)
  349.     PyObject *v, *w;
  350. {
  351.     BINOP(v, w, "__lshift__", "__rlshift__", PyNumber_Lshift);
  352.     if (v->ob_type->tp_as_number != NULL) {
  353.         PyObject *x = NULL;
  354.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  355.         if (PyNumber_Coerce(&v, &w) != 0)
  356.             return NULL;
  357.         if ((f = v->ob_type->tp_as_number->nb_lshift) != NULL)
  358.             x = (*f)(v, w);
  359.         Py_DECREF(v);
  360.         Py_DECREF(w);
  361.         if (f != NULL)
  362.             return x;
  363.     }
  364.     return type_error("bad operand type(s) for <<");
  365. }
  366.  
  367. PyObject *
  368. PyNumber_Rshift(v, w)
  369.     PyObject *v, *w;
  370. {
  371.     BINOP(v, w, "__rshift__", "__rrshift__", PyNumber_Rshift);
  372.     if (v->ob_type->tp_as_number != NULL) {
  373.         PyObject *x = NULL;
  374.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  375.         if (PyNumber_Coerce(&v, &w) != 0)
  376.             return NULL;
  377.         if ((f = v->ob_type->tp_as_number->nb_rshift) != NULL)
  378.             x = (*f)(v, w);
  379.         Py_DECREF(v);
  380.         Py_DECREF(w);
  381.         if (f != NULL)
  382.             return x;
  383.     }
  384.     return type_error("bad operand type(s) for >>");
  385. }
  386.  
  387. PyObject *
  388. PyNumber_Add(v, w)
  389.     PyObject *v, *w;
  390. {
  391.     PySequenceMethods *m;
  392.  
  393.     BINOP(v, w, "__add__", "__radd__", PyNumber_Add);
  394.     m = v->ob_type->tp_as_sequence;
  395.     if (m && m->sq_concat)
  396.         return (*m->sq_concat)(v, w);
  397.     else if (v->ob_type->tp_as_number != NULL) {
  398.         PyObject *x = NULL;
  399.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  400.         if (PyNumber_Coerce(&v, &w) != 0)
  401.             return NULL;
  402.         if ((f = v->ob_type->tp_as_number->nb_add) != NULL)
  403.             x = (*f)(v, w);
  404.         Py_DECREF(v);
  405.         Py_DECREF(w);
  406.         if (f != NULL)
  407.             return x;
  408.     }
  409.     return type_error("bad operand type(s) for +");
  410. }
  411.  
  412. PyObject *
  413. PyNumber_Subtract(v, w)
  414.     PyObject *v, *w;
  415. {
  416.     BINOP(v, w, "__sub__", "__rsub__", PyNumber_Subtract);
  417.     if (v->ob_type->tp_as_number != NULL) {
  418.         PyObject *x = NULL;
  419.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  420.         if (PyNumber_Coerce(&v, &w) != 0)
  421.             return NULL;
  422.         if ((f = v->ob_type->tp_as_number->nb_subtract) != NULL)
  423.             x = (*f)(v, w);
  424.         Py_DECREF(v);
  425.         Py_DECREF(w);
  426.         if (f != NULL)
  427.             return x;
  428.     }
  429.     return type_error("bad operand type(s) for -");
  430. }
  431.  
  432. PyObject *
  433. PyNumber_Multiply(v, w)
  434.     PyObject *v, *w;
  435. {
  436.     PyTypeObject *tp = v->ob_type;
  437.     PySequenceMethods *m;
  438.  
  439.     BINOP(v, w, "__mul__", "__rmul__", PyNumber_Multiply);
  440.     if (tp->tp_as_number != NULL &&
  441.         w->ob_type->tp_as_sequence != NULL &&
  442.         !PyInstance_Check(v)) {
  443.         /* number*sequence -- swap v and w */
  444.         PyObject *tmp = v;
  445.         v = w;
  446.         w = tmp;
  447.         tp = v->ob_type;
  448.     }
  449.     if (tp->tp_as_number != NULL) {
  450.         PyObject *x = NULL;
  451.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  452.         if (PyInstance_Check(v)) {
  453.             /* Instances of user-defined classes get their
  454.                other argument uncoerced, so they may
  455.                implement sequence*number as well as
  456.                number*number. */
  457.             Py_INCREF(v);
  458.             Py_INCREF(w);
  459.         }
  460.         else if (PyNumber_Coerce(&v, &w) != 0)
  461.             return NULL;
  462.         if ((f = v->ob_type->tp_as_number->nb_multiply) != NULL)
  463.             x = (*f)(v, w);
  464.         Py_DECREF(v);
  465.         Py_DECREF(w);
  466.         if (f != NULL)
  467.             return x;
  468.     }
  469.     m = tp->tp_as_sequence;
  470.     if (m && m->sq_repeat) {
  471.         if (!PyInt_Check(w))
  472.             return type_error(
  473.                 "can't multiply sequence with non-int");
  474.         return (*m->sq_repeat)(v, (int)PyInt_AsLong(w));
  475.     }
  476.     return type_error("bad operand type(s) for *");
  477. }
  478.  
  479. PyObject *
  480. PyNumber_Divide(v, w)
  481.     PyObject *v, *w;
  482. {
  483.     BINOP(v, w, "__div__", "__rdiv__", PyNumber_Divide);
  484.     if (v->ob_type->tp_as_number != NULL) {
  485.         PyObject *x = NULL;
  486.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  487.         if (PyNumber_Coerce(&v, &w) != 0)
  488.             return NULL;
  489.         if ((f = v->ob_type->tp_as_number->nb_divide) != NULL)
  490.             x = (*f)(v, w);
  491.         Py_DECREF(v);
  492.         Py_DECREF(w);
  493.         if (f != NULL)
  494.             return x;
  495.     }
  496.     return type_error("bad operand type(s) for /");
  497. }
  498.  
  499. PyObject *
  500. PyNumber_Remainder(v, w)
  501.     PyObject *v, *w;
  502. {
  503.     if (PyString_Check(v))
  504.         return PyString_Format(v, w);
  505.     BINOP(v, w, "__mod__", "__rmod__", PyNumber_Remainder);
  506.     if (v->ob_type->tp_as_number != NULL) {
  507.         PyObject *x = NULL;
  508.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  509.         if (PyNumber_Coerce(&v, &w) != 0)
  510.             return NULL;
  511.         if ((f = v->ob_type->tp_as_number->nb_remainder) != NULL)
  512.             x = (*f)(v, w);
  513.         Py_DECREF(v);
  514.         Py_DECREF(w);
  515.         if (f != NULL)
  516.             return x;
  517.     }
  518.     return type_error("bad operand type(s) for %");
  519. }
  520.  
  521. PyObject *
  522. PyNumber_Divmod(v, w)
  523.     PyObject *v, *w;
  524. {
  525.     BINOP(v, w, "__divmod__", "__rdivmod__", PyNumber_Divmod);
  526.     if (v->ob_type->tp_as_number != NULL) {
  527.         PyObject *x = NULL;
  528.         PyObject * (*f) Py_FPROTO((PyObject *, PyObject *));
  529.         if (PyNumber_Coerce(&v, &w) != 0)
  530.             return NULL;
  531.         if ((f = v->ob_type->tp_as_number->nb_divmod) != NULL)
  532.             x = (*f)(v, w);
  533.         Py_DECREF(v);
  534.         Py_DECREF(w);
  535.         if (f != NULL)
  536.             return x;
  537.     }
  538.     return type_error("bad operand type(s) for divmod()");
  539. }
  540.  
  541. /* Power (binary or ternary) */
  542.  
  543. static PyObject *
  544. do_pow(v, w)
  545.     PyObject *v, *w;
  546. {
  547.     PyObject *res;
  548.     PyObject * (*f) Py_FPROTO((PyObject *, PyObject *, PyObject *));
  549.     BINOP(v, w, "__pow__", "__rpow__", do_pow);
  550.     if (v->ob_type->tp_as_number == NULL ||
  551.         w->ob_type->tp_as_number == NULL) {
  552.         PyErr_SetString(PyExc_TypeError,
  553.                 "pow(x, y) requires numeric arguments");
  554.         return NULL;
  555.     }
  556.     if (PyNumber_Coerce(&v, &w) != 0)
  557.         return NULL;
  558.     if ((f = v->ob_type->tp_as_number->nb_power) != NULL)
  559.         res = (*f)(v, w, Py_None);
  560.     else
  561.         res = type_error("pow(x, y) not defined for these operands");
  562.     Py_DECREF(v);
  563.     Py_DECREF(w);
  564.     return res;
  565. }
  566.  
  567. PyObject *
  568. PyNumber_Power(v, w, z)
  569.     PyObject *v, *w, *z;
  570. {
  571.     PyObject *res;
  572.     PyObject *v1, *z1, *w2, *z2;
  573.     PyObject * (*f) Py_FPROTO((PyObject *, PyObject *, PyObject *));
  574.  
  575.     if (z == Py_None)
  576.         return do_pow(v, w);
  577.     /* XXX The ternary version doesn't do class instance coercions */
  578.     if (PyInstance_Check(v))
  579.         return v->ob_type->tp_as_number->nb_power(v, w, z);
  580.     if (v->ob_type->tp_as_number == NULL ||
  581.         z->ob_type->tp_as_number == NULL ||
  582.         w->ob_type->tp_as_number == NULL) {
  583.         return type_error("pow(x, y, z) requires numeric arguments");
  584.     }
  585.     if (PyNumber_Coerce(&v, &w) != 0)
  586.         return NULL;
  587.     res = NULL;
  588.     v1 = v;
  589.     z1 = z;
  590.     if (PyNumber_Coerce(&v1, &z1) != 0)
  591.         goto error2;
  592.     w2 = w;
  593.     z2 = z1;
  594.      if (PyNumber_Coerce(&w2, &z2) != 0)
  595.         goto error1;
  596.     if ((f = v1->ob_type->tp_as_number->nb_power) != NULL)
  597.         res = (*f)(v1, w2, z2);
  598.     else
  599.         res = type_error(
  600.             "pow(x, y, z) not defined for these operands");
  601.     Py_DECREF(w2);
  602.     Py_DECREF(z2);
  603.   error1:
  604.     Py_DECREF(v1);
  605.     Py_DECREF(z1);
  606.   error2:
  607.     Py_DECREF(v);
  608.     Py_DECREF(w);
  609.     return res;
  610. }
  611.  
  612. /* Unary operators and functions */
  613.  
  614. PyObject *
  615. PyNumber_Negative(o)
  616.     PyObject *o;
  617. {
  618.     PyNumberMethods *m;
  619.  
  620.     if (o == NULL)
  621.         return null_error();
  622.     m = o->ob_type->tp_as_number;
  623.     if (m && m->nb_negative)
  624.         return (*m->nb_negative)(o);
  625.  
  626.     return type_error("bad operand type for unary -");
  627. }
  628.  
  629. PyObject *
  630. PyNumber_Positive(o)
  631.     PyObject *o;
  632. {
  633.     PyNumberMethods *m;
  634.  
  635.     if (o == NULL)
  636.         return null_error();
  637.     m = o->ob_type->tp_as_number;
  638.     if (m && m->nb_positive)
  639.         return (*m->nb_positive)(o);
  640.  
  641.     return type_error("bad operand type for unary +");
  642. }
  643.  
  644. PyObject *
  645. PyNumber_Invert(o)
  646.     PyObject *o;
  647. {
  648.     PyNumberMethods *m;
  649.  
  650.     if (o == NULL)
  651.         return null_error();
  652.     m = o->ob_type->tp_as_number;
  653.     if (m && m->nb_invert)
  654.         return (*m->nb_invert)(o);
  655.  
  656.     return type_error("bad operand type for unary ~");
  657. }
  658.  
  659. PyObject *
  660. PyNumber_Absolute(o)
  661.     PyObject *o;
  662. {
  663.     PyNumberMethods *m;
  664.  
  665.     if (o == NULL)
  666.         return null_error();
  667.     m = o->ob_type->tp_as_number;
  668.     if (m && m->nb_absolute)
  669.         return m->nb_absolute(o);
  670.  
  671.     return type_error("bad operand type for abs()");
  672. }
  673.  
  674. PyObject *
  675. PyNumber_Int(o)
  676.     PyObject *o;
  677. {
  678.     PyNumberMethods *m;
  679.  
  680.     if (o == NULL)
  681.         return null_error();
  682.     if (PyString_Check(o))
  683.         return int_from_string(o);
  684.     m = o->ob_type->tp_as_number;
  685.     if (m && m->nb_int)
  686.         return m->nb_int(o);
  687.  
  688.     return type_error("object can't be converted to int");
  689. }
  690.  
  691. PyObject *
  692. PyNumber_Long(o)
  693.     PyObject *o;
  694. {
  695.     PyNumberMethods *m;
  696.  
  697.     if (o == NULL)
  698.         return null_error();
  699.     m = o->ob_type->tp_as_number;
  700.     if (m && m->nb_long)
  701.         return m->nb_long(o);
  702.  
  703.     return type_error("object can't be converted to long");
  704. }
  705.  
  706. PyObject *
  707. PyNumber_Float(o)
  708.     PyObject *o;
  709. {
  710.     PyNumberMethods *m;
  711.  
  712.     if (o == NULL)
  713.         return null_error();
  714.     if (PyString_Check(o))
  715.         return float_from_string(o);
  716.     m = o->ob_type->tp_as_number;
  717.     if (m && m->nb_float)
  718.         return m->nb_float(o);
  719.  
  720.     return type_error("object can't be converted to float");
  721. }
  722.  
  723. /* Operations on sequences */
  724.  
  725. int
  726. PySequence_Check(s)
  727.     PyObject *s;
  728. {
  729.     return s != NULL && s->ob_type->tp_as_sequence;
  730. }
  731.  
  732. int
  733. PySequence_Length(s)
  734.     PyObject *s;
  735. {
  736.     PySequenceMethods *m;
  737.  
  738.     if (s == NULL) {
  739.         null_error();
  740.         return -1;
  741.     }
  742.  
  743.     m = s->ob_type->tp_as_sequence;
  744.     if (m && m->sq_length)
  745.         return m->sq_length(s);
  746.  
  747.     type_error("len() of unsized object");
  748.     return -1;
  749. }
  750.  
  751. PyObject *
  752. PySequence_Concat(s, o)
  753.     PyObject *s;
  754.     PyObject *o;
  755. {
  756.     PySequenceMethods *m;
  757.  
  758.     if (s == NULL || o == NULL)
  759.         return null_error();
  760.  
  761.     m = s->ob_type->tp_as_sequence;
  762.     if (m && m->sq_concat)
  763.         return m->sq_concat(s, o);
  764.  
  765.     return type_error("object can't be concatenated");
  766. }
  767.  
  768. PyObject *
  769. PySequence_Repeat(o, count)
  770.     PyObject *o;
  771.     int count;
  772. {
  773.     PySequenceMethods *m;
  774.  
  775.     if (o == NULL)
  776.         return null_error();
  777.  
  778.     m = o->ob_type->tp_as_sequence;
  779.     if (m && m->sq_repeat)
  780.         return m->sq_repeat(o, count);
  781.  
  782.     return type_error("object can't be repeated");
  783. }
  784.  
  785. PyObject *
  786. PySequence_GetItem(s, i)
  787.     PyObject *s;
  788.     int i;
  789. {
  790.     PySequenceMethods *m;
  791.  
  792.     if (s == NULL)
  793.         return null_error();
  794.  
  795.     m = s->ob_type->tp_as_sequence;
  796.     if (m && m->sq_item) {
  797.         if (i < 0) {
  798.             if (m->sq_length) {
  799.                 int l = (*m->sq_length)(s);
  800.                 if (l < 0)
  801.                     return NULL;
  802.                 i += l;
  803.             }
  804.         }
  805.         return m->sq_item(s, i);
  806.     }
  807.  
  808.     return type_error("unindexable object");
  809. }
  810.  
  811. PyObject *
  812. PySequence_GetSlice(s, i1, i2)
  813.     PyObject *s;
  814.     int i1;
  815.     int i2;
  816. {
  817.     PySequenceMethods *m;
  818.  
  819.     if (!s) return null_error();
  820.  
  821.     m = s->ob_type->tp_as_sequence;
  822.     if (m && m->sq_slice) {
  823.         if (i1 < 0 || i2 < 0) {
  824.             if (m->sq_length) {
  825.                 int l = (*m->sq_length)(s);
  826.                 if (l < 0)
  827.                     return NULL;
  828.                 if (i1 < 0)
  829.                     i1 += l;
  830.                 if (i2 < 0)
  831.                     i2 += l;
  832.             }
  833.         }
  834.         return m->sq_slice(s, i1, i2);
  835.     }
  836.  
  837.     return type_error("unsliceable object");
  838. }
  839.  
  840. int
  841. PySequence_SetItem(s, i, o)
  842.     PyObject *s;
  843.     int i;
  844.     PyObject *o;
  845. {
  846.     PySequenceMethods *m;
  847.  
  848.     if (s == NULL) {
  849.         null_error();
  850.         return -1;
  851.     }
  852.  
  853.     m = s->ob_type->tp_as_sequence;
  854.     if (m && m->sq_ass_item) {
  855.         if (i < 0) {
  856.             if (m->sq_length) {
  857.                 int l = (*m->sq_length)(s);
  858.                 if (l < 0)
  859.                     return -1;
  860.                 i += l;
  861.             }
  862.         }
  863.         return m->sq_ass_item(s, i, o);
  864.     }
  865.  
  866.     type_error("object doesn't support item assignment");
  867.     return -1;
  868. }
  869.  
  870. int
  871. PySequence_DelItem(s, i)
  872.     PyObject *s;
  873.     int i;
  874. {
  875.     PySequenceMethods *m;
  876.  
  877.     if (s == NULL) {
  878.         null_error();
  879.         return -1;
  880.     }
  881.  
  882.     m = s->ob_type->tp_as_sequence;
  883.     if (m && m->sq_ass_item) {
  884.         if (i < 0) {
  885.             if (m->sq_length) {
  886.                 int l = (*m->sq_length)(s);
  887.                 if (l < 0)
  888.                     return -1;
  889.                 i += l;
  890.             }
  891.         }
  892.         return m->sq_ass_item(s, i, (PyObject *)NULL);
  893.     }
  894.  
  895.     type_error("object doesn't support item deletion");
  896.     return -1;
  897. }
  898.  
  899. int
  900. PySequence_SetSlice(s, i1, i2, o)
  901.     PyObject *s;
  902.     int i1;
  903.     int i2;
  904.     PyObject *o;
  905. {
  906.     PySequenceMethods *m;
  907.  
  908.     if (s == NULL) {
  909.         null_error();
  910.         return -1;
  911.     }
  912.  
  913.     m = s->ob_type->tp_as_sequence;
  914.     if (m && m->sq_ass_slice) {
  915.         if (i1 < 0 || i2 < 0) {
  916.             if (m->sq_length) {
  917.                 int l = (*m->sq_length)(s);
  918.                 if (l < 0)
  919.                     return -1;
  920.                 if (i1 < 0)
  921.                     i1 += l;
  922.                 if (i2 < 0)
  923.                     i2 += l;
  924.             }
  925.         }
  926.         return m->sq_ass_slice(s, i1, i2, o);
  927.     }
  928.     type_error("object doesn't support slice assignment");
  929.     return -1;
  930. }
  931.  
  932. int
  933. PySequence_DelSlice(s, i1, i2)
  934.     PyObject *s;
  935.     int i1;
  936.     int i2;
  937. {
  938.     PySequenceMethods *m;
  939.  
  940.     if (s == NULL) {
  941.         null_error();
  942.         return -1;
  943.     }
  944.  
  945.     m = s->ob_type->tp_as_sequence;
  946.     if (m && m->sq_ass_slice) {
  947.         if (i1 < 0 || i2 < 0) {
  948.             if (m->sq_length) {
  949.                 int l = (*m->sq_length)(s);
  950.                 if (l < 0)
  951.                     return -1;
  952.                 if (i1 < 0)
  953.                     i1 += l;
  954.                 if (i2 < 0)
  955.                     i2 += l;
  956.             }
  957.         }
  958.         return m->sq_ass_slice(s, i1, i2, (PyObject *)NULL);
  959.     }
  960.     type_error("object doesn't support slice deletion");
  961.     return -1;
  962. }
  963.  
  964. PyObject *
  965. PySequence_Tuple(v)
  966.     PyObject *v;
  967. {
  968.     PySequenceMethods *m;
  969.  
  970.     if (v == NULL)
  971.         return null_error();
  972.  
  973.     if (PyTuple_Check(v)) {
  974.         Py_INCREF(v);
  975.         return v;
  976.     }
  977.  
  978.     if (PyList_Check(v))
  979.         return PyList_AsTuple(v);
  980.  
  981.     /* There used to be code for strings here, but tuplifying strings is
  982.        not a common activity, so I nuked it.  Down with code bloat! */
  983.  
  984.     /* Generic sequence object */
  985.     m = v->ob_type->tp_as_sequence;
  986.     if (m && m->sq_item) {
  987.         int i;
  988.         PyObject *t;
  989.         int n = PySequence_Length(v);
  990.         if (n < 0)
  991.             return NULL;
  992.         t = PyTuple_New(n);
  993.         if (t == NULL)
  994.             return NULL;
  995.         for (i = 0; ; i++) {
  996.             PyObject *item = (*m->sq_item)(v, i);
  997.             if (item == NULL) {
  998.                 if (PyErr_ExceptionMatches(PyExc_IndexError))
  999.                     PyErr_Clear();
  1000.                 else {
  1001.                     Py_DECREF(t);
  1002.                     t = NULL;
  1003.                 }
  1004.                 break;
  1005.             }
  1006.             if (i >= n) {
  1007.                 if (n < 500)
  1008.                     n += 10;
  1009.                 else
  1010.                     n += 100;
  1011.                 if (_PyTuple_Resize(&t, n, 0) != 0)
  1012.                     break;
  1013.             }
  1014.             PyTuple_SET_ITEM(t, i, item);
  1015.         }
  1016.         if (i < n && t != NULL)
  1017.             _PyTuple_Resize(&t, i, 0);
  1018.         return t;
  1019.     }
  1020.  
  1021.     /* None of the above */
  1022.     return type_error("tuple() argument must be a sequence");
  1023. }
  1024.  
  1025. PyObject *
  1026. PySequence_List(v)
  1027.     PyObject *v;
  1028. {
  1029.     PySequenceMethods *m;
  1030.  
  1031.     if (v == NULL)
  1032.         return null_error();
  1033.  
  1034.     if (PyList_Check(v))
  1035.         return PyList_GetSlice(v, 0, PyList_GET_SIZE(v));
  1036.  
  1037.     m = v->ob_type->tp_as_sequence;
  1038.     if (m && m->sq_item) {
  1039.         int i;
  1040.         PyObject *l;
  1041.         int n = PySequence_Length(v);
  1042.         if (n < 0)
  1043.             return NULL;
  1044.         l = PyList_New(n);
  1045.         if (l == NULL)
  1046.             return NULL;
  1047.         for (i = 0; ; i++) {
  1048.             PyObject *item = (*m->sq_item)(v, i);
  1049.             if (item == NULL) {
  1050.                 if (PyErr_ExceptionMatches(PyExc_IndexError))
  1051.                     PyErr_Clear();
  1052.                 else {
  1053.                     Py_DECREF(l);
  1054.                     l = NULL;
  1055.                 }
  1056.                 break;
  1057.             }
  1058.             if (i < n)
  1059.                 PyList_SET_ITEM(l, i, item);
  1060.             else if (PyList_Append(l, item) < 0) {
  1061.                 Py_DECREF(l);
  1062.                 l = NULL;
  1063.                 break;
  1064.             }
  1065.         }
  1066.         if (i < n && l != NULL) {
  1067.             if (PyList_SetSlice(l, i, n, (PyObject *)NULL) != 0) {
  1068.                 Py_DECREF(l);
  1069.                 l = NULL;
  1070.             }
  1071.         }
  1072.         return l;
  1073.     }
  1074.     return type_error("list() argument must be a sequence");
  1075. }
  1076.  
  1077. int
  1078. PySequence_Count(s, o)
  1079.     PyObject *s;
  1080.     PyObject *o;
  1081. {
  1082.     int l, i, n, cmp, err;
  1083.     PyObject *item;
  1084.  
  1085.     if (s == NULL || o == NULL) {
  1086.         null_error();
  1087.         return -1;
  1088.     }
  1089.     
  1090.     l = PySequence_Length(s);
  1091.     if (l < 0)
  1092.         return -1;
  1093.  
  1094.     n = 0;
  1095.     for (i = 0; i < l; i++) {
  1096.         item = PySequence_GetItem(s, i);
  1097.         if (item == NULL)
  1098.             return -1;
  1099.         err = PyObject_Cmp(item, o, &cmp);
  1100.         Py_DECREF(item);
  1101.         if (err < 0)
  1102.             return err;
  1103.         if (cmp == 0)
  1104.             n++;
  1105.     }
  1106.     return n;
  1107. }
  1108.  
  1109. int
  1110. PySequence_Contains(w, v) /* v in w */
  1111.     PyObject *w;
  1112.     PyObject *v;
  1113. {
  1114.     int i, cmp;
  1115.     PyObject *x;
  1116.     PySequenceMethods *sq;
  1117.  
  1118.     /* Special case for char in string */
  1119.     if (PyString_Check(w)) {
  1120.         register char *s, *end;
  1121.         register char c;
  1122.         if (!PyString_Check(v) || PyString_Size(v) != 1) {
  1123.             PyErr_SetString(PyExc_TypeError,
  1124.                 "string member test needs char left operand");
  1125.             return -1;
  1126.         }
  1127.         c = PyString_AsString(v)[0];
  1128.         s = PyString_AsString(w);
  1129.         end = s + PyString_Size(w);
  1130.         while (s < end) {
  1131.             if (c == *s++)
  1132.                 return 1;
  1133.         }
  1134.         return 0;
  1135.     }
  1136.  
  1137.     sq = w->ob_type->tp_as_sequence;
  1138.     if (sq == NULL || sq->sq_item == NULL) {
  1139.         PyErr_SetString(PyExc_TypeError,
  1140.             "'in' or 'not in' needs sequence right argument");
  1141.         return -1;
  1142.     }
  1143.  
  1144.     for (i = 0; ; i++) {
  1145.         x = (*sq->sq_item)(w, i);
  1146.         if (x == NULL) {
  1147.             if (PyErr_ExceptionMatches(PyExc_IndexError)) {
  1148.                 PyErr_Clear();
  1149.                 break;
  1150.             }
  1151.             return -1;
  1152.         }
  1153.         cmp = PyObject_Compare(v, x);
  1154.         Py_XDECREF(x);
  1155.         if (cmp == 0)
  1156.             return 1;
  1157.         if (PyErr_Occurred())
  1158.             return -1;
  1159.     }
  1160.  
  1161.     return 0;
  1162. }
  1163.  
  1164. /* Backwards compatibility */
  1165. #undef PySequence_In
  1166. int
  1167. PySequence_In(w, v)
  1168.     PyObject *w;
  1169.     PyObject *v;
  1170. {
  1171.     return PySequence_Contains(w, v);
  1172. }
  1173.  
  1174. int
  1175. PySequence_Index(s, o)
  1176.     PyObject *s;
  1177.     PyObject *o;
  1178. {
  1179.     int l, i, cmp, err;
  1180.     PyObject *item;
  1181.  
  1182.     if (s == NULL || o == NULL) {
  1183.         null_error();
  1184.         return -1;
  1185.     }
  1186.     
  1187.     l = PySequence_Length(s);
  1188.     if (l < 0)
  1189.         return -1;
  1190.  
  1191.     for (i = 0; i < l; i++) {
  1192.         item = PySequence_GetItem(s, i);
  1193.         if (item == NULL)
  1194.             return -1;
  1195.         err = PyObject_Cmp(item, o, &cmp);
  1196.         Py_DECREF(item);
  1197.         if (err < 0)
  1198.             return err;
  1199.         if (cmp == 0)
  1200.             return i;
  1201.     }
  1202.  
  1203.     PyErr_SetString(PyExc_ValueError, "sequence.index(x): x not in list");
  1204.     return -1;
  1205. }
  1206.  
  1207. /* Operations on mappings */
  1208.  
  1209. int
  1210. PyMapping_Check(o)
  1211.     PyObject *o;
  1212. {
  1213.     return o && o->ob_type->tp_as_mapping;
  1214. }
  1215.  
  1216. int
  1217. PyMapping_Length(o)
  1218.     PyObject *o;
  1219. {
  1220.     PyMappingMethods *m;
  1221.  
  1222.     if (o == NULL) {
  1223.         null_error();
  1224.         return -1;
  1225.     }
  1226.  
  1227.     m = o->ob_type->tp_as_mapping;
  1228.     if (m && m->mp_length)
  1229.         return m->mp_length(o);
  1230.  
  1231.     type_error("len() of unsized object");
  1232.     return -1;
  1233. }
  1234.  
  1235. PyObject *
  1236. PyMapping_GetItemString(o, key)
  1237.     PyObject *o;
  1238.     char *key;
  1239. {
  1240.     PyObject *okey, *r;
  1241.  
  1242.     if (key == NULL)
  1243.         return null_error();
  1244.  
  1245.     okey = PyString_FromString(key);
  1246.     if (okey == NULL)
  1247.         return NULL;
  1248.     r = PyObject_GetItem(o, okey);
  1249.     Py_DECREF(okey);
  1250.     return r;
  1251. }
  1252.  
  1253. int
  1254. PyMapping_SetItemString(o, key, value)
  1255.     PyObject *o;
  1256.     char *key;
  1257.     PyObject *value;
  1258. {
  1259.     PyObject *okey;
  1260.     int r;
  1261.  
  1262.     if (key == NULL) {
  1263.         null_error();
  1264.         return -1;
  1265.     }
  1266.  
  1267.     okey = PyString_FromString(key);
  1268.     if (okey == NULL)
  1269.         return -1;
  1270.     r = PyObject_SetItem(o, okey, value);
  1271.     Py_DECREF(okey);
  1272.     return r;
  1273. }
  1274.  
  1275. int
  1276. PyMapping_HasKeyString(o, key)
  1277.     PyObject *o;
  1278.     char *key;
  1279. {
  1280.     PyObject *v;
  1281.  
  1282.     v = PyMapping_GetItemString(o, key);
  1283.     if (v) {
  1284.         Py_DECREF(v);
  1285.         return 1;
  1286.     }
  1287.     PyErr_Clear();
  1288.     return 0;
  1289. }
  1290.  
  1291. int
  1292. PyMapping_HasKey(o, key)
  1293.     PyObject *o;
  1294.     PyObject *key;
  1295. {
  1296.     PyObject *v;
  1297.  
  1298.     v = PyObject_GetItem(o, key);
  1299.     if (v) {
  1300.         Py_DECREF(v);
  1301.         return 1;
  1302.     }
  1303.     PyErr_Clear();
  1304.     return 0;
  1305. }
  1306.  
  1307. /* Operations on callable objects */
  1308.  
  1309. /* XXX PyCallable_Check() is in object.c */
  1310.  
  1311. PyObject *
  1312. PyObject_CallObject(o, a)
  1313.     PyObject *o, *a;
  1314. {
  1315.     PyObject *r;
  1316.     PyObject *args = a;
  1317.  
  1318.     if (args == NULL) {
  1319.         args = PyTuple_New(0);
  1320.         if (args == NULL)
  1321.             return NULL;
  1322.     }
  1323.  
  1324.     r = PyEval_CallObject(o, args);
  1325.  
  1326.     if (args != a) {
  1327.         Py_DECREF(args);
  1328.     }
  1329.  
  1330.     return r;
  1331. }
  1332.  
  1333. PyObject *
  1334. #ifdef HAVE_STDARG_PROTOTYPES
  1335. /* VARARGS 2 */
  1336. PyObject_CallFunction(PyObject *callable, char *format, ...)
  1337. #else
  1338. /* VARARGS */
  1339.     PyObject_CallFunction(va_alist) va_dcl
  1340. #endif
  1341. {
  1342.     va_list va;
  1343.     PyObject *args, *retval;
  1344. #ifdef HAVE_STDARG_PROTOTYPES
  1345.     va_start(va, format);
  1346. #else
  1347.     PyObject *callable;
  1348.     char *format;
  1349.     va_start(va);
  1350.     callable = va_arg(va, PyObject *);
  1351.     format   = va_arg(va, char *);
  1352. #endif
  1353.  
  1354.     if (callable == NULL) {
  1355.         va_end(va);
  1356.         return null_error();
  1357.     }
  1358.  
  1359.     if (format)
  1360.         args = Py_VaBuildValue(format, va);
  1361.     else
  1362.         args = PyTuple_New(0);
  1363.  
  1364.     va_end(va);
  1365.     
  1366.     if (args == NULL)
  1367.         return NULL;
  1368.  
  1369.     if (!PyTuple_Check(args)) {
  1370.         PyObject *a;
  1371.  
  1372.         a = PyTuple_New(1);
  1373.         if (a == NULL)
  1374.             return NULL;
  1375.         if (PyTuple_SetItem(a, 0, args) < 0)
  1376.             return NULL;
  1377.         args = a;
  1378.     }
  1379.     retval = PyObject_CallObject(callable, args);
  1380.  
  1381.     Py_DECREF(args);
  1382.  
  1383.     return retval;
  1384. }
  1385.  
  1386. PyObject *
  1387. #ifdef HAVE_STDARG_PROTOTYPES
  1388. /* VARARGS 2 */
  1389. PyObject_CallMethod(PyObject *o, char *name, char *format, ...)
  1390. #else
  1391. /* VARARGS */
  1392.     PyObject_CallMethod(va_alist) va_dcl
  1393. #endif
  1394. {
  1395.     va_list va;
  1396.     PyObject *args, *func = 0, *retval;
  1397. #ifdef HAVE_STDARG_PROTOTYPES
  1398.     va_start(va, format);
  1399. #else
  1400.     PyObject *o;
  1401.     char *name;
  1402.     char *format;
  1403.     va_start(va);
  1404.     o      = va_arg(va, PyObject *);
  1405.     name   = va_arg(va, char *);
  1406.     format = va_arg(va, char *);
  1407. #endif
  1408.  
  1409.     if (o == NULL || name == NULL) {
  1410.         va_end(va);
  1411.         return null_error();
  1412.     }
  1413.  
  1414.     func = PyObject_GetAttrString(o, name);
  1415.     if (func == NULL) {
  1416.         va_end(va);
  1417.         PyErr_SetString(PyExc_AttributeError, name);
  1418.         return 0;
  1419.     }
  1420.  
  1421.     if (!PyCallable_Check(func)) {
  1422.         va_end(va);
  1423.         return type_error("call of non-callable attribute");
  1424.     }
  1425.  
  1426.     if (format && *format)
  1427.         args = Py_VaBuildValue(format, va);
  1428.     else
  1429.         args = PyTuple_New(0);
  1430.  
  1431.     va_end(va);
  1432.  
  1433.     if (!args)
  1434.         return NULL;
  1435.  
  1436.     if (!PyTuple_Check(args)) {
  1437.         PyObject *a;
  1438.  
  1439.         a = PyTuple_New(1);
  1440.         if (a == NULL)
  1441.             return NULL;
  1442.         if (PyTuple_SetItem(a, 0, args) < 0)
  1443.             return NULL;
  1444.         args = a;
  1445.     }
  1446.  
  1447.     retval = PyObject_CallObject(func, args);
  1448.  
  1449.     Py_DECREF(args);
  1450.     Py_DECREF(func);
  1451.  
  1452.     return retval;
  1453. }
  1454.